package tiandang.core.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tiandang.core.dao.ManagerApplyMapper;
import tiandang.core.error.BusinessException;
import tiandang.core.error.EmBusinessError;
import tiandang.core.model.ManagerApply;
import tiandang.core.service.ManagerApplyService;
import tiandang.core.validator.ValidationResult;
import tiandang.core.validator.ValidatorImpl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ManagerApplyServiceImpl implements ManagerApplyService {

  @Autowired
  private ValidatorImpl validator;

  @Autowired
  private ManagerApplyMapper managerApplyMapper;

  @Override
  public Object getList(Integer currentPage, Integer limit) {
    int offset = (currentPage - 1) * limit;
    List<ManagerApply> managerApplyList = managerApplyMapper.getListByParams(limit, offset);
    int totalCount = managerApplyMapper.countAll();
    Map<String, Object> map = new HashMap<>();
    map.put("managerApplyList", managerApplyList);
    map.put("totalCount", totalCount);
    return map;
  }

  @Override
  public ManagerApply getManagerApplyById(Integer id) {
    return managerApplyMapper.selectByPrimaryKey(id);
  }

  @Override
  public void save(ManagerApply managerApply) throws BusinessException {
    //校验入参
    ValidationResult result = validator.validate(managerApply);
    if (result.isHasErrors()) {
      throw new BusinessException(EmBusinessError.PARAMMETER_VALIDATION_ERROR, result.getErrMsg());
    }
    managerApplyMapper.insertSelective(managerApply);
  }

  @Override
  public void delete(Integer id) {
    managerApplyMapper.deleteByPrimaryKey(id);
  }
}
